//
//  MineMessageListViewController.m
//  ShowYork
//
//  Created by hxh on 2020/11/13.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineMessageListViewController.h"
#import "MineCommentListViewController.h"
#import "MineDoLikeListViewController.h"
#import "MineAboutListViewController.h"
#import "MineMessageListCell.h"

#import "MineMessaeUnreadCountModel.h"

@interface MineMessageListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UIView *line;
@end

@implementation MineMessageListViewController




- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithObjects:@0,@0,@0,nil];
    }
    return _dataArray;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    UIImageView *navLine = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    navLine.hidden = YES;
    
    [self loadDataSource];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    UIImageView *navLine = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    navLine.hidden = NO;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    

    // Do any additional setup after loading the view.
}

- (void)setUI{
    self.navigationItem.title = @"消息";
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];

    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.view addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(0.5);
    }];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
}


- (void)loadDataSource{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = [RPAppManager getCustomerId];
    param[@"md5"] = [RPAppManager md5StrWithApi:MessageTypeUnRead];
    [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(MessageTypeUnRead) parames:param success:^(id responseObj) {
        NSArray *resultArray = [MineMessaeUnreadCountModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE]];
        
        __block BOOL isUnreadMessage = NO;
        __block NSInteger maxUnreadCount = 0;
        self.dataArray = [NSMutableArray arrayWithObjects:@0,@0,@0,nil];
        
        
        [resultArray enumerateObjectsUsingBlock:^(MineMessaeUnreadCountModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.messageType.intValue == 1) {
                [self.dataArray replaceObjectAtIndex:0 withObject:obj.messageUrNum];
            }
            if (obj.messageType.intValue == 2) {
                [self.dataArray replaceObjectAtIndex:1 withObject:obj.messageUrNum];
            }
            if (obj.messageType.intValue == 3) {
                [self.dataArray replaceObjectAtIndex:2 withObject:obj.messageUrNum];
            }
            
            if (obj.messageUrNum.intValue > 0) {
                isUnreadMessage = YES;
            }
            maxUnreadCount += obj.messageUrNum.intValue;
        }];
//        if (isUnreadMessage) {
//            [self.tabBarController.tabBar showBadgeOnItemIndex:3];
//        }else{
//            [self.tabBarController.tabBar hideBadgeOnItemIndex:3];
//        }
        
        [[NSUserDefaults standardUserDefaults]setObject:@(isUnreadMessage) forKey:@"messageReadStatus"];
        [[NSUserDefaults standardUserDefaults]setObject:@(maxUnreadCount) forKey:@"messageUnReadCount"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [WHToast showMessage:error.userInfo[@"info"] duration:2 finishHandler:nil];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MineMessageListCell *cell = [MineMessageListCell cellWithTableView:tableView];
    if (indexPath.row == 0) {
        [cell setMessageTitle:@"赞" icon:@"mine_dolike_tag" bageValue:self.dataArray[indexPath.row]];
    }
    if (indexPath.row == 1) {
        [cell setMessageTitle:@"@我的" icon:@"mine_about_tag" bageValue:self.dataArray[indexPath.row]];
    }
    if (indexPath.row == 2) {
        [cell setMessageTitle:@"评论" icon:@"mine_comment_tag" bageValue:self.dataArray[indexPath.row]];
    }
  
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        MineDoLikeListViewController *likeVC = [[MineDoLikeListViewController alloc]init];
        [self.navigationController pushViewController:likeVC animated:YES];
    }
    if (indexPath.row == 1) {
        MineAboutListViewController *aboutVC = [[MineAboutListViewController alloc]init];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    if (indexPath.row == 2) {
        MineCommentListViewController *commentVC = [[MineCommentListViewController alloc]init];
        [self.navigationController pushViewController:commentVC animated:YES];
    }
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

@end
